package com.smartmedi.ai.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smartmedi.ai.common.BaseResponse;
import com.smartmedi.ai.common.DeleteRequest;
import com.smartmedi.ai.common.ResultUtils;
import com.smartmedi.ai.domain.dto.JhInMrRequsetPage;
import com.smartmedi.ai.domain.entity.JhInMr;
import com.smartmedi.ai.domain.vo.JhInMrVo;
import com.smartmedi.ai.exception.ErrorCode;
import com.smartmedi.ai.exception.ThrowUtils;
import com.smartmedi.ai.service.JhInMrService;
import com.smartmedi.common.annotation.DataSource;
import com.smartmedi.common.enums.DataSourceType;
import com.smartmedi.common.utils.bean.BeanUtils;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;


@RestController
@RequestMapping("/JhIn")
@RequiredArgsConstructor
@Slf4j
public class JhInController {

    private final JhInMrService jhInMrService;

    /**
     * 分页查询住院电子病历记录列表
     * @param jhInMrRequsetPage 查询参数
     * @return 列表
     */
    @RequestMapping("/list")
    @DataSource(value = DataSourceType.SLAVE)
    public BaseResponse<Page<JhInMrVo>> list(JhInMrRequsetPage jhInMrRequsetPage) {
        // 参数校验
        ThrowUtils.throwIf(jhInMrRequsetPage == null, ErrorCode.PARAMS_ERROR, "查询参数不能为空");

        // 分页参数校验
        long current = jhInMrRequsetPage.getCurrent();
        long pageSize = jhInMrRequsetPage.getPageSize();
        ThrowUtils.throwIf(current <= 0, ErrorCode.PARAMS_ERROR, "当前页码必须大于0");
        ThrowUtils.throwIf(pageSize <= 0 || pageSize > 100, ErrorCode.PARAMS_ERROR, "每页数量必须在1-100之间");

        try {
            // 构建查询条件
            QueryWrapper<JhInMr> queryWrapper = jhInMrService.getQueryWrapper(jhInMrRequsetPage);

            // 分页查询
            Page<JhInMr> jhInMrPage = jhInMrService.page(new Page<>(current, pageSize), queryWrapper);

            // 转换为VO对象
            Page<JhInMrVo> jhInMrVoPage = new Page<>(current, pageSize);
            jhInMrVoPage.setTotal(jhInMrPage.getTotal());
            jhInMrVoPage.setRecords(jhInMrPage.getRecords().stream().map(jhInMr -> {
                JhInMrVo jhInMrVo = new JhInMrVo();
                BeanUtils.copyProperties(jhInMr, jhInMrVo);
                return jhInMrVo;
            }).collect(Collectors.toList()));

            System.out.println("分页查询结果：" + jhInMrVoPage);

            return ResultUtils.success(jhInMrVoPage);
        } catch (Exception e) {
            log.error("分页查询住院电子病历列表失败: {}", e.getMessage(), e);
            return (BaseResponse<Page<JhInMrVo>>) ResultUtils.error(ErrorCode.SYSTEM_ERROR, "查询列表失败，请稍后重试");
        }
    }

    /**
     * 查询电子病历记录详情
     * @param rowkey rowkey
     * @return 电子病历记录详情
     */
    @GetMapping("/detail")
    @Operation(summary = "查询电子病历记录详情")
    @DataSource(value = DataSourceType.SLAVE)
    public BaseResponse<JhInMr> getJhInMrById(String rowkey) {
        ThrowUtils.throwIf(rowkey == null, ErrorCode.PARAMS_ERROR);
        JhInMr jhInMrResult = jhInMrService.getById(rowkey);

        System.out.println(jhInMrResult);
        return ResultUtils.success(jhInMrResult);
    }

    /**
     * 创建
     * @param jhInMr 新的电子病历记录
     * @return 创建的电子病历记录
     * */
    @PostMapping("/add")
    @Operation(summary = "创建电子病历记录")
    @DataSource(value = DataSourceType.SLAVE)
    public BaseResponse<Long> addJhInMr(@RequestBody JhInMr jhInMr) {

        ThrowUtils.throwIf(jhInMr == null, ErrorCode.PARAMS_ERROR);
        Long newJhInMrId = jhInMrService.addJhInMr(jhInMr);
        return ResultUtils.success(newJhInMrId);
    }

    /**
     * 删除电子记录
     * @param deleteRequest 删除请求
     * @return 删除结果
     */
    @DeleteMapping("/delete")
    @Operation(summary = "删除")
    @DataSource(value = DataSourceType.SLAVE)
    public BaseResponse<Boolean> deleteJhInMr(@RequestBody DeleteRequest deleteRequest) {
        ThrowUtils.throwIf(deleteRequest == null || deleteRequest.getId() <= 0, ErrorCode.PARAMS_ERROR);
        boolean b = jhInMrService.removeById(deleteRequest.getId());
        return ResultUtils.success(b);
    }

    /**
     * 更新
     * @param jhInMr 更新数据
     * @return 更新成功与否
     */
    @PostMapping("/update")
    @Operation(summary = "更新")
    @DataSource(value = DataSourceType.SLAVE)
    public BaseResponse<Boolean> updateJhInMr(@RequestBody JhInMr jhInMr) {
        ThrowUtils.throwIf(jhInMr == null || jhInMr.getRowkey() <= 0, ErrorCode.PARAMS_ERROR);
        boolean b = jhInMrService.updateById(jhInMr);
        return ResultUtils.success(b);
    }

}
